<template>
  <view>
    <uni-section title="服务评价" type="line">
      <view style="width: 90%;margin: auto">
        <uni-forms ref="baseForm" :modelValue="evaluateData" label-width="100">
          <uni-forms-item label="服务评价等级" required>
            <uni-rate v-model="evaluateData.evaluateGrade"/>
          </uni-forms-item>
          <uni-forms-item label="评价内容" required>
            <uni-easyinput type="textarea" v-model="evaluateData.evaluateContent" placeholder="请输入评价内容"/>
          </uni-forms-item>
        </uni-forms>
        <button type="primary" @click="submit('baseForm')">提交</button>
      </view>
    </uni-section>
  </view>
</template>

<script>
import {RepairEvaluateService} from "./repairEvaluateService";

export default {
  data() {
    return {
      evaluateData: {
        evaluateGrade: 0,
        evaluateContent: "",
        userId: uni.getStorageSync('userInfo').eid,
        repairmanId: "",
        repairOrderId: ""
      },
    }
  },
  onLoad(options) {
    // var data = options.id
    // console.log(options)
    if(!options.eid) {
      //新增
      this.evaluateData.repairOrderId = options.id;
      this.evaluateData.repairUserId = options.repairmanId
    }else{
      //修改
      this.init(options.eid)
    }
  },
  methods: {
    init(eid){
      // console.log("eid--",eid)
      RepairEvaluateService.findRepairEvaluateForView(eid).then(res=>{
        this.evaluateData=res.data;
        // console.log(res)
      })
    },
    submit(ref) {
      this.$refs[ref].validate().then(res => {
        if(this.evaluateData.eid){
          this.update()
          return;
        }
        this.add();
      }).catch(err => {
        console.log('err', err);
      })
    },
    add(){
      RepairEvaluateService.saveRepairEvaluate(this.evaluateData).then(res => {
        if (res.statusCode === 200&&res.data.code!=1) {
          uni.showToast({
            title: `操作成功`
          })
          this.back();
        }
      }).catch(err => {
        uni.showToast({
          title: err.message
        })
      })
    },
    update(){
      RepairEvaluateService.updateRepairEvaluate(this.evaluateData).then(res => {
        if (res.statusCode === 200) {
          uni.showToast({
            title: `操作成功`
          })
          this.back();
        }
      }).catch(err => {
        uni.showToast({
          title: err.message
        })
      })
    },
    back(){
      let pages = getCurrentPages(); // 当前页面
      let beforePage = pages[pages.length - 2]; // 上一页
      if(beforePage.$vm.$refs.repairOrder) {
        beforePage.$vm.$refs.repairOrder.refreshIfNeeded = true;
      }
      uni.navigateBack({
        delta:1,//返回层数，2则上上页
      })
    }
  }
}
</script>

<style>

</style>
